using UnityEngine;

namespace CGF
{
    //在Hierarcy窗口节点右键--CGF组件---ScrollView即可快创建，也可在ScrollRect节点上直接绑定ScrollView组件
    public class ScrollViewDemo : MonoBehaviour
    {
        public ScrollView scrollView;
        public int total = 1, changeTo = 20;
        public int jumpToIndex;
        string[] data;

        void Start()
        {
            //模拟列表尽可能大的数据
            data = new string[total];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = $"{i}";
            }

            //绑定事件
            scrollView.SetCellUpdateCallback<ScrollViewDemoCell>(OnCellUpdate);
            scrollView.SetTotalNumber(data.Length);
        }

        private void OnCellUpdate(ScrollViewDemoCell cell, int index)
        {
            //更新Cell内容，支持不同Cell不同尺寸
            cell.text.text = data[index];
            if (index %5 == 0)
            {
                //(cell.transform as RectTransform).sizeDelta = new Vector2(350, 250);
            }
            else
            {
                //(cell.transform as RectTransform).sizeDelta = new Vector2(100, 100);
            }
        }

        void Update()
        {
            if (Input.GetMouseButtonDown(1))
            {
                //3种快速定位到
                //scrollView.JumpToHead();
                //scrollView.JumpToEnd();
                //scrollView.ScrollToIndex(jumpToIndex);

                //改变数据
                data = new string[changeTo];
                for (int i = 0; i < data.Length; i++)
                {
                    data[i] = $"{i}";
                }
                scrollView.SetTotalNumber(data.Length);
            }
        }
    }
}
